package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;
import com.heima.model.admin.dtos.UserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.admin.vos.UserVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {

    /**
     * 用户登录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(UserDto dto) {
        //1，校验数据
        if (StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "用户名或密码不能为空！");
        }
        //2,校验用户信息是否正确
        AdUser user = this.getOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, dto.getName()));
        String password = user.getPassword();
        String md5DigestAsHex = DigestUtils.md5DigestAsHex((dto.getPassword() + user.getSalt()).getBytes());
        if (!password.equals(md5DigestAsHex)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR, "用户名或密码错误");
        }
        //用户信息正确,颁发令牌
        String token = AppJwtUtil.getToken(user.getId().longValue());
        //修改登录时间
        user.setLoginTime(new Date());
        this.updateById(user);

        //封装数据返回给前台
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);

        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", userVo);

        return ResponseResult.okResult(map);
    }

    /**
     * 用户注册
     * @param adUser
     * @return
     */
    @Override
    public ResponseResult register(AdUser adUser) {
        //1,校验数据
        if (adUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        List<AdUser> list = this.list(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, adUser.getName()));
        if (list.size()>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //2，对输入的密码进行加密
        String string = RandomStringUtils.randomAlphabetic(5);
        String password = adUser.getPassword();
        String md5DigestAsHex = DigestUtils.md5DigestAsHex((password + string).getBytes());
        adUser.setPassword(md5DigestAsHex);
        //3,将用户信息存入
        adUser.setCreatedTime(new Date());
        adUser.setSalt(string);
        this.save(adUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
